﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;
using System.IO;
using ComLib.ComType;

namespace KInterfaces
{
    public class ShareDataManager : Singleton<ShareDataManager>
    {
        public ShareDataManager()
        {
            _dicCollection.Add("global", _global);
        }

        private Dictionary<string, ShareData> _dicCollection = new Dictionary<string, ShareData>();

        private static ShareData _global = new ShareData();
        public static ShareData GlobalData
        {
            get { return _global; }
        }

        public ShareData this[string key]
        {
            get 
            {
                if (!_dicCollection.ContainsKey(key))
                {
                    _dicCollection.Add(key, new ShareData());
                }
                return _dicCollection[key];
            }
            set 
            {
                if (!_dicCollection.ContainsKey(key))
                {
                    _dicCollection.Add(key, new ShareData());
                }
                _dicCollection[key] = value;
            }
        }

        public ShareData Add(string key)
        {
            if (!_dicCollection.ContainsKey(key))
            {
                _dicCollection.Add(key, new ShareData());
            }
            return _dicCollection[key];
        }

        public void Remove(string key)
        {
            if (_dicCollection.ContainsKey(key))
            {
                _dicCollection.Remove(key);
            }
        }

        public void Reset(string key)
        {
            if (_dicCollection.ContainsKey(key))
            {
                _dicCollection[key].ClearValue();
            }
        }

        public void ResetAll()
        {
            foreach (KeyValuePair<string, ShareData> p in _dicCollection)
            {
                ShareData data = p.Value;
                data.ClearValue();
           }
        }
    }

    /// <summary>
    /// 各模块共享的数据类
    /// </summary>
    public class ShareData
    {

        /// <summary>
        /// 值列表
        /// </summary>
        public Hashtable htValues = new Hashtable();        
  
        public ShareData()
        {
            
        }
        
        /// <summary>
        /// 更新指定编号的值数据
        /// </summary>
        /// <param name="ValueId">值编号</param>
        /// <param name="NewValue">值数据</param>
        /// <param name="NotExistThenAdd">不存在则添加</param>
        public void SetValue(string ValueId, object NewValue, bool NotExistThenAdd = true)
        {
            try
            {
                if (htValues[ValueId] != null)
                    htValues[ValueId] = NewValue;
                else if (NotExistThenAdd)
                    htValues.Add(ValueId, NewValue);
            }
            catch
            {
            }
        }
        
        /// <summary>
        /// 获取指定Id号的值
        /// </summary>
        /// <param name="FormId"></param>
        /// <returns></returns>
        public T GetValue<T>(string ValueId, object NullReturn = null)
        {
            try
            {
                if (htValues==null)
                {
                    return default(T);
                }
                if (!htValues.ContainsKey(ValueId))
                {
                    if (NullReturn != null && NullReturn.GetType() == typeof(T))
                    {
                        return (T)NullReturn;
                    }
                    else
                    {
                        return default(T);
                    }
                }
                object obj = htValues[ValueId];
                if (obj != null && obj.GetType() == typeof(T))
                {
                    return (T)obj;
                }
                else if (NullReturn != null && NullReturn.GetType() == typeof(T))
                {
                    return (T)NullReturn;
                }
                else
                {
                    return default(T);
                }                                   
            }
            catch
            {
                return default(T);
            }
        }        

        /// <summary>
        /// 移除指定Id号的值
        /// </summary>
        /// <param name="FormId"></param>
        public void Remove(string ValueId)
        {
            if (htValues.ContainsKey(ValueId))
            {
                htValues.Remove(ValueId);
            }           
        }

        public bool ContainsKey(string key)
        {
            return htValues.ContainsKey(key);
        }

        public void ClearValue()
        {
            htValues.Clear();
        }
    }    
}
